<?php

namespace App\Admin\Controllers;

use \App\Booking;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use function foo\func;
use Illuminate\Http\Request;

class BookingController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('入住管理')
            ->description('管理入住信息')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('明细')
            ->description('入住记录明细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('编辑入住信息')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建')
            ->description('新建入住信息')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Booking);

        $grid->id('Id');
        $grid->room()->number('房间号');
        $grid->check_in('入住日期');
        $grid->customer()->name('客户姓名');
        $grid->customer()->phone('客户电话');
        $grid->customer()->gender('客户性别')->using([0 => '男', 1 => '女'])->sortable();
        $grid->availability()->to('预计退房日期');
        $grid->column('状态')->display(function (){
            if($this->check_out !== null) {
                return '已退房';
            }
            return "<a href='" . route('admin.check_out', ['id' => $this->id]) . "' role='button' class='btn btn-danger'>立即退房</a>";
        });

        //filter
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter){
                $filter->where(function ($query){
                    if($this->input == true){
                        $query->where('check_out', '!=', null);
                    } else if ($this->input == false){
                        $query->where('check_out', null);
                    }
                }, '已退房')->radio([
                    '' => '所有',
                    true => '是',
                    false => '否'
                ]);
            });
            $filter->column(1/2, function ($filter){
                $filter->where(function ($query){
                    $query->whereHas('customer', function ($query){
                        $query->where('name', $this->input);
                    });
                }, '客户姓名');
                $filter->where(function ($query){
                    $query->whereHas('customer',function ($query){
                        $query->where('phone', $this->input);
                    });
                }, '客户电话');
            });
        });
        $grid->disableRowSelector();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Booking::findOrFail($id));

        $show->id('Id');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        $show->room_id('Room id');
        $show->check_in('Check in');
        $show->check_out('Check out');
        $show->customer_id('Customer id');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Booking);

        $form->number('room_id', 'Room id');
        $form->date('check_in', 'Check in')->default(date('Y-m-d'));
        $form->date('check_out', 'Check out')->default(date('Y-m-d'));
        $form->number('customer_id', 'Customer id');

        return $form;
    }

    public function check_out(Request $request){
        $id = $request->get('id');
        $booking = Booking::findOrFail($id);
        // invalidate the correspond availability info
        $booking->availability->is_valid = false;
        $booking->availabilibty->save();
        // if check out is before 'to'
        // if it is current date
        // TODO:处理提前退房的支付问题
        $booking->check_out = date('today');
        $booking->save();
        return back();
    }
}
